#!/bin/bash
 #克隆 uir 仓库
# 尝试克隆仓库，最多重试5次
MAX_ATTEMPTS=20
# 初始尝试次数为1
ATTEMPT_NUMBER=1
# 仓库源码路径
# 克隆源码
function clone_repo(){
until git $1; do
    if [ $ATTEMPT_NUMBER -lt $MAX_ATTEMPTS ]; then
        echo $2
        echo "克隆失败，第 $ATTEMPT_NUMBER 次重试..."
        let ATTEMPT_NUMBER++
        sleep 10 # 等待5秒后再重试
    else
        echo $2
        echo "重试次数已到，停止尝试。"
        exit 1
    fi
done

}


function config_ffmpeg()
{
# 设置 lib 存放目录目录
PREFIX=${BUILD_DIR}/android/${CPU}/$API
 # 设置动态库路径
PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig
# 进入 ffmpeg 目录
cd $BUILD_DIR/ffmpeg
CXX=$TOOLCHAIN/bin/$ARCH_TYPE$API-clang++
CC=$TOOLCHAIN/bin/$ARCH_TYPE$API-clang
echo "开始配置 configure ffmpeg"
./configure \
--prefix=$PREFIX \
--arch=$ARCH \
--target-os=linux \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/llvm- \
--sysroot=$SYSTEMROOT \
--cxx=$CXX \
--cc=$CC \
--nm=$TOOLCHAIN/bin/llvm-nm \
--extra-cxxflags="-D__thumb__ -fexceptions -frtti" \
--extra-cflags="$CFLAGS -Os -fPIC -DANDROID -Wfatal-errors -Wno-deprecated -I${PREFIX}/include" \
--extra-ldflags="-L$SYSTEMROOT/usr/lib -L${PREFIX}/lib" \
--pkg-config="pkg-config --shared" \
--disable-static \
--enable-shared \
--enable-gpl \
--enable-version3 \
--enable-runtime-cpudetect \
--enable-small \
--enable-network \
--disable-iconv \
--enable-asm \
--enable-neon \
--enable-yasm \
--disable-encoders \
--enable-encoder=h263 \
--enable-encoder=libx264 \
--enable-encoder=aac \
--enable-encoder=mpeg4 \
--enable-encoder=mjpeg \
--enable-encoder=png \
--enable-encoder=gif \
--enable-encoder=bmp \
--disable-muxers \
--enable-muxer=h264 \
--enable-muxer=flv \
--enable-muxer=gif \
--enable-muxer=mp3 \
--enable-muxer=dts \
--enable-muxer=mp4 \
--enable-muxer=mov \
--enable-muxer=mpegts \
--disable-decoders \
--enable-decoder=aac \
--enable-decoder=aac_latm \
--enable-decoder=mp3 \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-decoder=mjpeg \
--enable-decoder=gif \
--enable-decoder=png \
--enable-decoder=bmp \
--enable-decoder=yuv4 \
--disable-demuxers \
--enable-demuxer=image2 \
--enable-demuxer=h263 \
--enable-demuxer=h264 \
--enable-demuxer=flv \
--enable-demuxer=gif \
--enable-demuxer=aac \
--enable-demuxer=ogg \
--enable-demuxer=dts \
--enable-demuxer=mp3 \
--enable-demuxer=mov \
--enable-demuxer=m4v \
--enable-demuxer=concat \
--enable-demuxer=mpegts \
--enable-demuxer=mjpeg \
--enable-demuxer=mpegvideo \
--enable-demuxer=rawvideo \
--enable-demuxer=yuv4mpegpipe \
--enable-demuxer=rtsp \
--disable-parsers \
--enable-parser=aac \
--enable-parser=ac3 \
--enable-parser=h264 \
--enable-parser=mjpeg \
--enable-parser=png \
--enable-parser=bmp\
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--disable-protocols \
--enable-protocol=file \
--enable-protocol=hls \
--enable-protocol=concat \
--enable-protocol=rtp \
--enable-protocol=rtmp \
--enable-protocol=rtmpt \
--disable-filters \
--disable-filters \
--enable-filter=aresample \
--enable-filter=asetpts \
--enable-filter=setpts \
--enable-filter=ass \
--enable-filter=scale \
--enable-filter=concat \
--enable-filter=atempo \
--enable-filter=movie \
--enable-filter=overlay \
--enable-filter=rotate \
--enable-filter=transpose \
--enable-filter=hflip \
--disable-outdevs \
--disable-doc \
--disable-ffplay \
--disable-ffmpeg \
--disable-debug \
--disable-ffprobe \
--disable-postproc \
--enable-avdevice \
--disable-symver \
--enable-zlib \
--disable-stripping


make clean
make -j8
make install
}

# 编译 x264
function config_x264()
{

export CC=$TOOLCHAIN/bin/$ARCH_TYPE$API-clang
export CXX=$TOOLCHAIN/bin/$ARCH_TYPE$API-clang++
cd ${BUILD_DIR}/x264
  # 设置 lib 存放目录目录
PREFIX=${BUILD_DIR}/android/${CPU}/$API
./configure \
--prefix=$PREFIX \
--disable-cli \
--enable-static \
--enable-shared \
--enable-pic \
--host=$host \
--cross-prefix=$TOOLCHAIN/bin/$CROSS \
--sysroot=$SYSTEMROOT \

make clean
make -j8
make install
}


#  编译 x265
function config_x265() {
  # 设置 x265 源码路径
  X265_DIR=${BUILD_DIR}/x265
  # 判断 x264 源码路径 文件夹是否存在
  if [ ! -d ${X265_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 x265
    clone_repo "clone https://gitcode.com/sound/x265.git ${X265_DIR}" "克隆 x265"
  fi

# 进入  x265 目录
cd ${X265_DIR}/build/linux
# 添加编译工具目录
export CC=$ARCH_TYPE$API-clang
export CXX=$ARCH_TYPE$API-clang++
export AR=$ARCH_TYPE-ar
export LD=$ARCH_TYPE-ld
export ASM=$ARCH_TYPE-as
# 设置 lib 存放目录目录
PREFIX=${BUILD_DIR}/android/${CPU}/$API
cmake -G "Unix Makefiles" \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DCMAKE_SYSTEM_NAME=Android \
-DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang \
-DCMAKE_ANDROID_ARCH_ABI=${CPU} \
-DCMAKE_ANDROID_NDK=$NDK \
-DCMAKE_ANDROID_STL_TYPE=c++_shared \
-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
-DCMAKE_C_COMPILER=$CC \
-DCMAKE_CXX_COMPILER=$CXX \
-DCMAKE_ASM_COMPILER=$ASM \
-DCMAKE_AR=$AR \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_MAKE_PROGRAM=/usr/bin/make \
-DENABLE_SHARED=on -DENABLE_CLI=off ../../source

# 修改生成动态库配置文件
sed -i 's/-lpthread/-pthread/g' CMakeFiles/cli.dir/link.txt
sed -i 's/-lpthread/-pthread/g' CMakeFiles/x265-shared.dir/link.txt
sed -i 's/-lpthread/-pthread/g' CMakeFiles/x265-static.dir/link.txt
make clean
make -j$(nproc)
make install

}

#  编译 openssl
function config_openssl() {
  # 设置 openssl 源码路径
  OPENSSL_DIR=${BUILD_DIR}/openssl
  # 判断 openssl 源码路径 文件夹是否存在
  if [ ! -d ${OPENSSL_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 openssl
    clone_repo "clone -b OpenSSL_1_1_1-stable https://gitcode.com/sound/openssl.git ${OPENSSL_DIR}" "克隆 openssl"
  fi
  #进入 openssl 源码路径
  cd ${OPENSSL_DIR}
    echo "编译 openssl"
    ./Configure android-armv7a \
    --prefix=${BUILD_DIR}/android/${CPU}/$API \
    --enable-static \
    --enable-pic \
    --disable-asm \
    --disable-doc \
    --disable-cli \
    --host=arm-linux \
    --cross-prefix=$TOOLCHAIN/bin/$ARCH_TYPE \
    --sysroot=$SYSTEMROOT
    make clean
    make -j$(nproc)
    make install
}

#  编译 fdk-aac
function config_fdk-aac() {
  # 设置 fdk-aac 源码路径
  FDKAAC_DIR=${BUILD_DIR}/fdkaac
  # 判断 fdk-aac 源码路径 文件夹是否存在
  if [ ! -d ${FDKAAC_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 mp3lame
    clone_repo "clone https://gitcode.com/sound/fdk-aac.git ${FDKAAC_DIR}" "克隆 fdk-aac"
  fi
  #进入 fdk-aac 源码路径
  cd ${FDKAAC_DIR}
    echo "编译 fdk-aac"
    ./autogen.sh
    ./configure --prefix=${LIB_DIR} --enable-shared
    make clean
    make -j4
    make install
}

















